package actions;

import model.UserModel;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DBModel;

import forms.UserFormUpdate;

@WebServlet
public class ShowUserAction extends Action{
	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,HttpServletResponse response){
		try{
			
			DBModel db = new DBModel();
			UserModel user;
			String adminparameter=request.getParameter("name");
			String userName = request.getUserPrincipal().getName();
			if(adminparameter==null){
				user=db.getUser(userName);
			}else{
				user=db.getUser(adminparameter);
			}
			 //Gets the username of this user from the session with the server
			UserFormUpdate u = (UserFormUpdate) form;
			u.setUserId(String.valueOf(user.getId()));
			u.setEmail(user.getEmail());
			u.setBio(user.getBio());	
			
			request.setAttribute("admin", adminparameter);
			//request.getRequestDispatcher("user.jsp?").forward(request, response);
			return mapping.findForward("success");
		}catch (Exception e){
			e.printStackTrace();
			request.setAttribute("error", e.toString());
			return mapping.findForward("fail");
		}	
	}

}
